package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.domain.FlyTo;
import com.landinfo.service.FlyToService;
import com.landinfo.web.rest.errors.BadRequestAlertException;
import com.landinfo.web.rest.util.HeaderUtil;
import com.landinfo.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing FlyTo.
 */
@RestController
@RequestMapping("/api")
public class FlyToResource {

    private final Logger log = LoggerFactory.getLogger(FlyToResource.class);

    private static final String ENTITY_NAME = "flyTo";

    private final FlyToService flyToService;

    public FlyToResource(FlyToService flyToService) {
        this.flyToService = flyToService;
    }

    /**
     * POST  /fly-tos : Create a new flyTo.
     *
     * @param flyTo the flyTo to create
     * @return the ResponseEntity with status 201 (Created) and with body the new flyTo, or with status 400 (Bad Request) if the flyTo has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/fly-tos")
    @Timed
    public ResponseEntity<FlyTo> createFlyTo(@Valid @RequestBody FlyTo flyTo) throws URISyntaxException {
        log.debug("REST request to save FlyTo : {}", flyTo);
        if (flyTo.getId() != null) {
            throw new BadRequestAlertException("A new flyTo cannot already have an ID", ENTITY_NAME, "idexists");
        }
        FlyTo result = flyToService.save(flyTo);
        return ResponseEntity.created(new URI("/api/fly-tos/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /fly-tos : Updates an existing flyTo.
     *
     * @param flyTo the flyTo to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated flyTo,
     * or with status 400 (Bad Request) if the flyTo is not valid,
     * or with status 500 (Internal Server Error) if the flyTo couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/fly-tos")
    @Timed
    public ResponseEntity<FlyTo> updateFlyTo(@Valid @RequestBody FlyTo flyTo) throws URISyntaxException {
        log.debug("REST request to update FlyTo : {}", flyTo);
        if (flyTo.getId() == null) {
            return createFlyTo(flyTo);
        }
        FlyTo result = flyToService.save(flyTo);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, flyTo.getId().toString()))
            .body(result);
    }

    /**
     * GET  /fly-tos : get all the flyTos.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of flyTos in body
     */
    @GetMapping("/fly-tos")
    @Timed
    public ResponseEntity<List<FlyTo>> getAllFlyTos(Pageable pageable) {
        log.debug("REST request to get a page of FlyTos");
        Page<FlyTo> page = flyToService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/fly-tos");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /fly-tos/:id : get the "id" flyTo.
     *
     * @param id the id of the flyTo to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the flyTo, or with status 404 (Not Found)
     */
    @GetMapping("/fly-tos/{id}")
    @Timed
    public ResponseEntity<FlyTo> getFlyTo(@PathVariable Long id) {
        log.debug("REST request to get FlyTo : {}", id);
        FlyTo flyTo = flyToService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(flyTo));
    }

    /**
     * DELETE  /fly-tos/:id : delete the "id" flyTo.
     *
     * @param id the id of the flyTo to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/fly-tos/{id}")
    @Timed
    public ResponseEntity<Void> deleteFlyTo(@PathVariable Long id) {
        log.debug("REST request to delete FlyTo : {}", id);
        flyToService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
